package JUnit;

import static org.junit.Assert.assertTrue;

import org.junit.Before;
import org.junit.Test;

import protocol.Headers;
import Servlets.WhatsAppConstatnts;

public class add_user extends mainTest {

	@Before
	public void setUp() throws Exception {
		_url = "add_user";
	}

	@Test
	public void testERROR_242() {
		String response1 = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=yochailehman2&Phone=0546861622\n$"));
		String cookie = this.getCookie(response1);
		String response2 = this.runtest(new RunnableClient("localhost", 9969,
				"POST /" + _url + ".jsp HTTP/1.1\nCookie: " + cookie
						+ "\n\nUserName=yochailehman2&Phone=0546861622\n$"));
		assertTrue(response2.contains(WhatsAppConstatnts.ERROR_242));
	}

	@Test
	public void testERROR_770() {
		String response1 = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=yochailehman2&Phone=0546861622\n$"));
		String cookie = this.getCookie(response1);
		String response2 = this.runtest(new RunnableClient("localhost", 9969,
				"POST /" + _url + ".jsp HTTP/1.1\nCookie: " + cookie
						+ "\n\nTarget=aaaaRRRaaaa&User=0546861622\n$"));
		assertTrue(response2.contains(WhatsAppConstatnts.ERROR_770));
	}

	@Test
	public void test403() {
		String response1 = this
				.runtest(new RunnableClient(
						"localhost",
						9969,
						"POST /"
								+ _url
								+ ".jsp HTTP/1.1\n\nUserName=yochailehman2&Phone=0546861622\n$"));
		assertTrue(response1.contains(Headers.STATUS_403));
	}

	@Test
	public void test405() {
		String response1 = this
				.runtest(new RunnableClient(
						"localhost",
						9969,
						"GET /"
								+ _url
								+ ".jsp HTTP/1.1\n\nUserName=yochailehman2&Phone=0546861622\n$"));
		assertTrue(response1.contains("HTTP/1.1 " + Headers.STATUS_405));
	}

	@Test
	public void testERROR_669() {
		String response5 = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=userToAdd&Phone=123456\n$"));
		String cookie2 = this.getCookie(response5);
		String response1 = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=yochailehman2&Phone=0546861622\n$"));
		String cookie = this.getCookie(response1);
		this.runtest(new RunnableClient("localhost", 9969,
				"POST /create_group.jsp HTTP/1.1\nCookie: " + cookie2 + "\n\n"
						+ WhatsAppConstatnts.GROUP_NAME
						+ "=TestGroup1&Users=userToAdd\n$"));
		String response3 = this.runtest(new RunnableClient("localhost", 9969,
				"POST /" + _url + ".jsp HTTP/1.1\nCookie: " + cookie
						+ "\n\nTarget=TestGroup1&User=0546861622\n$"));
		assertTrue(response3.contains(WhatsAppConstatnts.ERROR_669));
	}

	@Test
	public void testERROR_142() {
		String response5 = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=userToAdd&Phone=123456\n$"));
		String cookie2 = this.getCookie(response5);
		String response1 = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=yochailehman2&Phone=0546861622\n$"));
		String cookie = this.getCookie(response1);
		this.runtest(new RunnableClient("localhost", 9969,
				"POST /create_group.jsp HTTP/1.1\nCookie: " + cookie + "\n\n"
						+ WhatsAppConstatnts.GROUP_NAME
						+ "=TestGroup1&Users=userToAdd,yochailehman2\n$"));
		String response3 = this.runtest(new RunnableClient("localhost", 9969,
				"POST /" + _url + ".jsp HTTP/1.1\nCookie: " + cookie2
						+ "\n\nTarget=TestGroup1&User=0546861622\n$"));
		assertTrue(response3.contains(WhatsAppConstatnts.ERROR_142));
	}
	
	@Test
	public void testERROR_142_userAddedInAddUser() {
		String response5 = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=userToAdd&Phone=123456\n$"));
		String cookie2 = this.getCookie(response5);
		String response1 = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=yochailehman2&Phone=0546861622\n$"));
		String cookie = this.getCookie(response1);
		this.runtest(new RunnableClient("localhost", 9969,
				"POST /create_group.jsp HTTP/1.1\nCookie: " + cookie + "\n\n"
						+ WhatsAppConstatnts.GROUP_NAME
						+ "=TestGroup1&Users=userToAdd\n$"));
		String response3 = this.runtest(new RunnableClient("localhost", 9969,
				"POST /" + _url + ".jsp HTTP/1.1\nCookie: " + cookie2
						+ "\n\nTarget=TestGroup1&User=0546861622\n$"));
		String response4 = this.runtest(new RunnableClient("localhost", 9969,
				"POST /" + _url + ".jsp HTTP/1.1\nCookie: " + cookie2
						+ "\n\nTarget=TestGroup1&User=0546861622\n$"));
		assertTrue(response3.contains("0546861622 added to TestGroup1"));
		assertTrue(response4.contains(WhatsAppConstatnts.ERROR_142));
	}

	@Test
	public void test() {
		String response5 = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=userToAdd&Phone=123456\n$"));
		String cookie2 = this.getCookie(response5);
		String response1 = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=yochailehman2&Phone=0546861622\n$"));
		
		String response7 = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=testUser&Phone=4444\n$"));
		String cookie = this.getCookie(response1);
		this.runtest(new RunnableClient("localhost", 9969,
				"POST /create_group.jsp HTTP/1.1\nCookie: " + cookie + "\n\n"
						+ WhatsAppConstatnts.GROUP_NAME
						+ "=TestGroup1&Users=userToAdd\n$"));
		String response3 = this.runtest(new RunnableClient("localhost", 9969,
				"POST /" + _url + ".jsp HTTP/1.1\nCookie: " + cookie2
						+ "\n\nTarget=TestGroup1&User=4444\n$"));
		assertTrue(response3.contains("4444 added to TestGroup1"));
	}
}
